﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Drawing;

namespace DX1
{
    class MySphere : MyObject
    {
        //constructor
        public MySphere(ref Device d) : base(ref d) { }

        public Vector3 Spherical(float r, float theta, float phi)
        {
            Vector3 pt = new Vector3();
            float snt = (float)Math.Sin(theta * Math.PI / 180);
            float cnt = (float)Math.Cos(theta * Math.PI / 180);
            float snp = (float)Math.Sin(phi * Math.PI / 180);
            float cnp = (float)Math.Cos(phi * Math.PI / 180);
            
            pt.X = r * snt * cnp;
            pt.Y = r * cnt;
            pt.Z = -r * snt * snp;
            
            return pt;
        }



        public void CreateMySphere(float r)
        {
            int N = 250;

            NoV = N * N;
            int NoF = (NoV) * 6;

            float twoPi = (float)Math.PI * 2;

            vertices = new CustomVertex.PositionNormalTextured[NoV];



            #region vertices


            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    vertices[N * i + j].Position = Spherical(
                        r,
                        i * 180 / (N - 1),
                        j * 360 / (N - 1));
                }
            }


            #endregion

            int[] indices = new int[NoF];

            for (int i = 1; i < N; i++)
            {
                for (int j = 1; j < N; j++)
                {
                    int x = 6 * ((N) * (i - 1) + (j - 1));

                    indices[x] = (i - 1) * N + j - 1;
                    indices[x + 1] = (i) * N + j - 1;
                    indices[x + 2] = (i) * N + j;

                    indices[x + 3] = (i - 1) * N + j;
                    indices[x + 4] = (i - 1) * N + j - 1;
                    indices[x + 5] = (i) * N + j;
                }
            }

            for (int i = 0; i < NoV; i++)
            {
                vertices[i].Tu = (float)Math.Acos(vertices[i].Y / r) / twoPi;
                vertices[i].Tv = (float)Math.Acos(vertices[i].X / r / Math.Sin(vertices[i].Tu * twoPi)) / twoPi;
            }


            m = new Mesh(NoF, NoV, MeshFlags.Managed | MeshFlags.Use32Bit, CustomVertex.PositionNormalTextured.Format, dev);

            m.IndexBuffer.SetData(indices, 0, LockFlags.None);
            m.VertexBuffer.SetData(vertices, 0, LockFlags.None);

            m.ComputeNormals();
            CalculateBounds();
            ExtendedMaterials = new ExtendedMaterial[1];
            Textures = new Texture[1];
            Materials = new Material[1];
            ExtendedMaterials[0] = new ExtendedMaterial();
            Textures[0] = null;
            Materials[0] = new Material();
            Materials[0].Ambient = Color.White;
            Materials[0].Diffuse = Color.White;
        }
    }
}
